package com.aochen.demo0419;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.aochen.demo0419.helper.OnDragVHListener;

import java.util.List;

import javax.security.auth.login.LoginException;

// TODO: 2018/4/20 0020
/*1.点击下半部分不能增加到上半部分
2.下半部分右上角图片动态显示
3.下半部分可以跨页面添加*/


/**
 * 拖拽排序 + 增删
 * Created by YoKeyword on 15/12/28.
 */
public class ChannelAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> /*implements OnItemMoveListener*/ {

    public static final String TAG = ChannelAdapter.class.getSimpleName();
    // 我的频道 标题部分
    public static final int TYPE_MY_CHANNEL_HEADER = 0;
    // 我的频道
    public static final int TYPE_MY = 1;
    // 其他频道 标题部分
    public static final int TYPE_OTHER_CHANNEL_HEADER = 2;
    // 其他频道
    public static final int TYPE_OTHER = 3;

    // 我的频道之前的header数量  该demo中 即标题部分 为 1
    private static final int COUNT_PRE_MY_HEADER = 1;
    // 其他频道之前的header数量  该demo中 即标题部分 为 COUNT_PRE_MY_HEADER + 1
    private static final int COUNT_PRE_OTHER_HEADER = COUNT_PRE_MY_HEADER + 1;

    private LayoutInflater mInflater;
    private ItemTouchHelper mItemTouchHelper;
    private Context mContext;
    //记录是否选择过
    private boolean isSelected ;

    private List<ChannelEntity> mMyChannelItems, mOtherChannelItems;


    public ChannelAdapter(Context context, ItemTouchHelper helper, List<ChannelEntity> mMyChannelItems, List<ChannelEntity> mOtherChannelItems) {
        this.mInflater = LayoutInflater.from(context);
        this.mItemTouchHelper = helper;
        this.mMyChannelItems = mMyChannelItems;
        this.mOtherChannelItems = mOtherChannelItems;
        this.mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {    // 我的频道 标题部分
            return TYPE_MY_CHANNEL_HEADER;
        } else if (position == mMyChannelItems.size() + 1) {    // 其他频道 标题部分
            return TYPE_OTHER_CHANNEL_HEADER;
        } else if (position > 0 && position < mMyChannelItems.size() + 1) {
            return TYPE_MY;
        } else {
            return TYPE_OTHER;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        final View view;
        switch (viewType) {
            case TYPE_MY_CHANNEL_HEADER:
                view = mInflater.inflate(R.layout.item_my_channel_header, parent, false);
                MyChannelHeaderViewHolder holder = new MyChannelHeaderViewHolder(view);
                return holder;

            case TYPE_MY:
                view = mInflater.inflate(R.layout.item_my, parent, false);
                final MyViewHolder myHolder = new MyViewHolder(view);

                myHolder.textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(final View v) {
                        if (mOtherChannelItems.size()>=5){
                            Toast.makeText(mContext,"最多只能选择五个",Toast.LENGTH_SHORT).show();
                            return;
                        }
                        isSelected = !isSelected;
                        if (!isSelected){
                            myHolder.textView.setBackgroundResource(R.drawable.shape_corner_gray_10);
                            /*for (int i = 0; i <= mOtherChannelItems.size()-1 ; i++) {
                                if (mOtherChannelItems.size()>0&&mOtherChannelItems.get(i).getName().equals(myHolder.textView.getText())){
                                    mOtherChannelItems.remove(i);
                                    Log.e(TAG, "onClick: "+mOtherChannelItems.get(i).getName()+i);
                                    //notifyItemChanged(mMyChannelItems.size()-1+COUNT_PRE_OTHER_HEADER,mMyChannelItems.size()-1+COUNT_PRE_OTHER_HEADER+i);
                                }
                            }*/
                            //removeOtherOne(myHolder.getAdapterPosition());
                        }else {
                            myHolder.textView.setBackgroundResource(R.drawable.shape_corner_red_10);
                            moveMyToOther(myHolder);
                        }
                    }
                });

                return myHolder;

            case TYPE_OTHER_CHANNEL_HEADER:
                view = mInflater.inflate(R.layout.item_other_channel_header, parent, false);
                return new RecyclerView.ViewHolder(view) {
                };

            case TYPE_OTHER:
                view = mInflater.inflate(R.layout.item_other, parent, false);
                final OtherViewHolder otherHolder = new OtherViewHolder(view);
                otherHolder.textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO: 2018/4/20 0020
                        moveOtherToMy(otherHolder);
                        //removeOtherOne(otherHolder.getAdapterPosition());
                        Toast.makeText(parent.getContext(), "点击了" + otherHolder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
                    }
                });
                otherHolder.imgEdit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //moveOtherToMy(otherHolder);
                    }
                });
                return otherHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {

            MyViewHolder myHolder = (MyViewHolder) holder;
            myHolder.textView.setText(mMyChannelItems.get(position - COUNT_PRE_MY_HEADER).getName());

        } else if (holder instanceof OtherViewHolder) {

            ((OtherViewHolder) holder).textView.setText(mOtherChannelItems.get(position - mMyChannelItems.size() - COUNT_PRE_OTHER_HEADER).getName());
        } else if (holder instanceof MyChannelHeaderViewHolder) {

            MyChannelHeaderViewHolder headerHolder = (MyChannelHeaderViewHolder) holder;
        }
    }

   /* //设置下半部分右上角叉叉图片的可见性，只有最后一个条目的图片才能显示
    private void setDelVisiblity(OtherViewHolder holder,int position) {
        Log.e(TAG, "onBindViewHolder: " + position + "----" + (mOtherChannelItems.size() - 1));
        if ((position - mMyChannelItems.size() - COUNT_PRE_OTHER_HEADER) == mOtherChannelItems.size() - 1) {
            ((OtherViewHolder) holder).imgEdit.setVisibility(View.VISIBLE);
        } else {
            ((OtherViewHolder) holder).imgEdit.setVisibility(View.INVISIBLE);
        }
    }*/

  /* public void removeOtherOne(int position){
       //移除other下半部分的一个条目 需要刷新

       //position必须足够大 保证是other的条目
       if (position<mMyChannelItems.size()+COUNT_PRE_OTHER_HEADER){
           for (int i = 0; i <= mOtherChannelItems.size()-1 ; i++) {
               if (mOtherChannelItems.get(i).getName().equals(mMyChannelItems.get(position).getName())){
                   mOtherChannelItems.remove(i);
               }
           }
           notifyItemChanged(mMyChannelItems.size()+COUNT_PRE_OTHER_HEADER,mMyChannelItems.size()+COUNT_PRE_OTHER_HEADER+mOtherChannelItems.size());
       }else {
           mOtherChannelItems.remove(position-mMyChannelItems.size()+COUNT_PRE_OTHER_HEADER);
       }
       //notifyDataSetChanged();
       notifyItemChanged(position-1);
   }*/

    @Override
    public int getItemCount() {
        // 我的频道  标题 + 我的频道.size + 其他频道 标题 + 其他频道.size
        return mMyChannelItems.size() + mOtherChannelItems.size() + COUNT_PRE_OTHER_HEADER;
    }

    /**
     * 我的频道 移动到 其他频道
     *
     * @param myHolder
     */
    private void moveMyToOther(MyViewHolder myHolder) {
        int position = myHolder.getAdapterPosition();

        int startPosition = position - COUNT_PRE_MY_HEADER;
        if (startPosition > mMyChannelItems.size() - 1) {
            return;
        }
        ChannelEntity item = mMyChannelItems.get(startPosition);
        for (int i = 0; i <mOtherChannelItems.size() ; i++) {
            if (item.getName().equals(mOtherChannelItems.get(i).getName())){
                return;
            }
        }
        mOtherChannelItems.add(0, item);
        notifyItemMoved(mMyChannelItems.size()+1, mMyChannelItems.size() + COUNT_PRE_OTHER_HEADER);
        //Log.e(TAG, "moveMyToOther: " + position + "----" + (mMyChannelItems.size() + COUNT_PRE_OTHER_HEADER));
    }

    /**
     * 其他频道 移动到 我的频道
     *
     * @param otherHolder
     */
    private void moveOtherToMy(OtherViewHolder otherHolder) {
        int position = processItemRemoveAdd(otherHolder);
        if (position == -1) {
            return;
        }
        notifyItemMoved(position, mMyChannelItems.size() - 1 + COUNT_PRE_MY_HEADER);
        //Log.e(TAG, "moveOtherToMy: " + position + "----" + (mMyChannelItems.size() - 1 + COUNT_PRE_MY_HEADER));
    }


    private int processItemRemoveAdd(OtherViewHolder otherHolder) {
        int position = otherHolder.getAdapterPosition();

        int startPosition = position - mMyChannelItems.size() - COUNT_PRE_OTHER_HEADER;
        if (startPosition > mOtherChannelItems.size() - 1) {
            return -1;
        }
        ChannelEntity item = mOtherChannelItems.get(startPosition);
        mOtherChannelItems.remove(startPosition);
        Log.e(TAG, "processItemRemoveAdd: "+mOtherChannelItems.size() );
        mMyChannelItems.add(item);

        /*for (int i = 0; i < mOtherChannelItems.size(); i++) {
            if (i==(mOtherChannelItems.size()-1)){
                otherHolder.imgEdit.setVisibility(View.VISIBLE);
            }else {
                otherHolder.imgEdit.setVisibility(View.INVISIBLE);
            }
        }*/
        Log.e(TAG, "processItemRemoveAdd: " + position + "----" + startPosition);
        return position;
    }

    /**
     * 我的频道
     */
    class MyViewHolder extends RecyclerView.ViewHolder implements OnDragVHListener {
        private TextView textView;

        public MyViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.tv);
        }

        /**
         * item 被选中时
         */
        @Override
        public void onItemSelected() {
            textView.setBackgroundResource(R.drawable.bg_channel_p);
        }

        /**
         * item 取消选中时
         */
        @Override
        public void onItemFinish() {
            textView.setBackgroundResource(R.drawable.bg_channel);
        }
    }

    /**
     * 其他频道
     */
    class OtherViewHolder extends RecyclerView.ViewHolder {
        private TextView textView;
        private ImageView imgEdit;

        public OtherViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.tv);
            imgEdit = (ImageView) itemView.findViewById(R.id.img_edit);
        }
    }

    /**
     * 我的频道  标题部分
     */
    class MyChannelHeaderViewHolder extends RecyclerView.ViewHolder {
        public MyChannelHeaderViewHolder(View itemView) {
            super(itemView);
        }
    }
}
